"""
@Filename: tools/md5
@Author: yoko
@Time: 2023/3/27 16:14
@Describe: ...
"""
import hashlib
from pathlib import Path
from pprint import pprint

import jsonpath
import yaml

from tools.operate_yaml import write_yaml
class Tool:

    # md5加密
    def md5_encode(self,args):
        # 把变量转化成utf-8编码格式
        args=str(args).encode("utf-8")#encode是代码进行编码
        # md5加密
        md5_value=hashlib.md5(args).hexdigest()
        return md5_value
    # Jsonpath提取数据
    def js_extract(self,res,variable,expression):
        """
        :param res: 返回的响应信息
        :param variable:变量名
        :param expression: Jsonpath提取数据表达式
        :return:
        """
        value=jsonpath.jsonpath(res.json(),expression)
        write_yaml('temp/temporary_variable.yaml',variable,value)
    # 读yaml文件
    def read_key_value(self,yaml_path,key):
        path=Path(yaml_path)
        with open(path.absolute(),encoding="utf-8") as f:
            value=yaml.safe_load(f)
            return value[key]
